package com.hdwmw;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.hdwmw.constant.Constant;
import com.hdwmw.entity.UserInfo;
import com.hdwmw.json.JsonParser;
import com.hdwmw.net.HttpUtil;
import com.hdwmw.util.SharePreferenceUtil;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Personal extends BaseActivity {
	String userID;
	UserInfo usreInfo;
	
	TextView userName;
	TextView phone;
	TextView address;
	TextView jf;
	
	ReceChangeDate bro;
	
	Button exit;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		userID = SharePreferenceUtil.getUserID(this);
		if(!userID.equals("")) {
			setContentView(R.layout.person_activity);
			initView();
			new GetUserInfo().execute(Constant.url + Constant.qryUserInfo);
		}
		
		bro = new ReceChangeDate();
		IntentFilter filter = new IntentFilter("com.personal.data");
		registerReceiver(bro, filter);
		
	}

	private void initView() {
		userName = (TextView) findViewById(R.id.userName);
		phone = (TextView) findViewById(R.id.phone);
		address = (TextView) findViewById(R.id.address);
		jf = (TextView) findViewById(R.id.jf);
		
		exit = (Button) findViewById(R.id.back);
		
		exit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				/*SharePreferenceUtil.exit(Personal.this);
				Intent in = new Intent(Personal.this, Main_1.class);
				startActivity(in);*/
				finish();
			}
		});
	}

	@Override
	protected void onResume() {
		if(usreInfo == null && !userID.equals("")) {
			setContentView(R.layout.person_activity);
			initView();
			new GetUserInfo().execute(Constant.url + Constant.qryUserInfo);
		}
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		unregisterReceiver(bro);
		super.onDestroy();
	}


	class GetUserInfo extends AsyncTask<String, Void, Void> {

		@Override
		protected Void doInBackground(String... arg0) {
			String url = arg0[0];
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			NameValuePair param = new BasicNameValuePair("userid",userID);
			params.add(param);
			String result = HttpUtil.post(url, params);
			usreInfo = JsonParser.getUser(result);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			if(usreInfo != null) {
				userName.setText(usreInfo.getUserName());
				phone.setText(usreInfo.getNumber());
				address.setText(usreInfo.getAddress());
				jf.setText(usreInfo.getJifen());
			}
			super.onPostExecute(result);
		}
		
	}
	
	
	class ReceChangeDate extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			new GetUserInfo().execute(Constant.url + Constant.qryUserInfo);
		}
		
	}
	
}
